//
//  CS_QueryPanel_MultiSelectView.m
//  CSUIKit
//
//  Created by 浙大网新中研软件 on 2017/11/10.
//

#import "CS_QueryPanel_MultiSelectView.h"

#define MARGIN 10.0
#define HEIGHT_CONDITION_BUTTON 35.0

@interface CS_QueryPanel_MultiSelectView()

@property (nonatomic, strong) NSMutableArray *selectItemButtons;

@property (nonatomic, strong) NSMutableArray *selectableModels;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UIButton *clearButton;

@property (nonatomic, strong) UIButton *saveButton;

@end

@implementation CS_QueryPanel_MultiSelectView

+(id)queryPanel_MultiSelectView:(CSConditionModel *)conditionModel{
    
    CS_QueryPanel_MultiSelectView *query_RadioView = [CS_QueryPanel_MultiSelectView new];
    query_RadioView.conditionModel = conditionModel;
    return  query_RadioView;
    
}

-(CGFloat)layoutView:(UIView *)superView primaryY:(CGFloat)primaryY{
    
    if (self.conditionModel == nil) {
        return primaryY;
    }
    
    CGFloat localPrimaryY = 0;
    CGFloat primaryX = 0;
    
    if ([self isHashSubItems]) {
        
        //选项分级处理
        for (int i = 0; i < self.conditionModel.selectItems.count; i++) {
            
            CSSelectableModel *selectableModel = [self.conditionModel.selectItems objectAtIndex:i];
            if (selectableModel.subItems == nil || selectableModel.subItems <= 0) {
                continue;
            }
            
            CS_Page_SectionTitleView *page_SectionTitleView = [CS_Page_SectionTitleView page_SectionTitleView:selectableModel.content color:COLOR_TEXT_2 image:nil rightButtonName:@"" rightClickHandler:^{
                
            }];
            page_SectionTitleView.font = [UIFont systemFontOfSize:FONT_SIZE_2];
            
            page_SectionTitleView.isTopSeperatorLineHide = YES;
            
            localPrimaryY = [page_SectionTitleView layoutView:self.scrollView primaryY:localPrimaryY];
            
            primaryX = 0;
            
            //选项不分级处理
            for (int i = 0; i < selectableModel.subItems.count; i++) {
                
                CSSelectableModel *subSelectableModel = [selectableModel.subItems objectAtIndex:i];
                
                NSString *buttonTitle = @"";
                
                buttonTitle = subSelectableModel.content;
                
                UIButton *conditionButton = [self getConditionButton:buttonTitle isSelected:subSelectableModel.isSelected];
                
                if (conditionButton.frame.size.width + primaryX + MARGIN > kWindowW - 2 *CELL_EDIT_RIGHT_MARGIN) {
                    
                    localPrimaryY += HEIGHT_CONDITION_BUTTON + MARGIN;
                    primaryX = 0;
                    
                }
                
                conditionButton.frame = CGRectMake(primaryX + CELL_EDIT_RIGHT_MARGIN, localPrimaryY, conditionButton.frame.size.width, conditionButton.frame.size.height);
                [self.scrollView addSubview:conditionButton];
                primaryX += conditionButton.frame.size.width + MARGIN;
                
                [self.selectItemButtons addObject:conditionButton];
                [self.selectableModels addObject:subSelectableModel];
                
            }
            
            //如果可选项大于0个
            localPrimaryY += HEIGHT_CONDITION_BUTTON + MARGIN;
        }
        
    }else{
        
        //选项不分级处理
        for (int i = 0; i < self.conditionModel.selectItems.count; i++) {
            
            CSSelectableModel *selectableModel = [self.conditionModel.selectItems objectAtIndex:i];
            
            NSString *buttonTitle = @"";

            buttonTitle = selectableModel.content;
            
            UIButton *conditionButton = [self getConditionButton:buttonTitle isSelected:selectableModel.isSelected];
            
            if (conditionButton.frame.size.width + primaryX + MARGIN > kWindowW - 2 *CELL_EDIT_RIGHT_MARGIN) {
                
                localPrimaryY += HEIGHT_CONDITION_BUTTON + MARGIN;
                primaryX = 0;
                
            }
            
            conditionButton.frame = CGRectMake(primaryX + CELL_EDIT_RIGHT_MARGIN, localPrimaryY, conditionButton.frame.size.width, conditionButton.frame.size.height);
            [self.scrollView addSubview:conditionButton];
            primaryX += conditionButton.frame.size.width + MARGIN;
            
            [self.selectItemButtons addObject:conditionButton];
            [self.selectableModels addObject:selectableModel];
            
        }
    }
    
    localPrimaryY += MARGIN;
    
    CGFloat buttonWidth = (kWindowW - 2 * CELL_EDIT_RIGHT_MARGIN - MARGIN) / 2.0;
    self.clearButton.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, localPrimaryY, buttonWidth, HEIGHT_CONDITION_BUTTON);
    self.saveButton.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN + buttonWidth + MARGIN, localPrimaryY, buttonWidth, HEIGHT_CONDITION_BUTTON);
    [self.scrollView addSubview:self.clearButton];
    [self.scrollView addSubview:self.saveButton];
    localPrimaryY += 55;
    
    self.backgroundColor = [UIColor whiteColor];
    
    CGFloat localHeight = localPrimaryY;
    
    if (localPrimaryY > kWindowH - primaryY - 150) {
        localHeight = kWindowH - primaryY - 150;
        
        //背景视图
        UIView *backView = [UIView new];
        backView.backgroundColor = [UIColor whiteColor];
        backView.frame = CGRectMake(0, localHeight - 55, kWindowW, 55);
        [self addSubview:backView];
        
        self.clearButton.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN, 10, buttonWidth, HEIGHT_CONDITION_BUTTON);
        self.saveButton.frame = CGRectMake(CELL_EDIT_RIGHT_MARGIN + buttonWidth + MARGIN, 10, buttonWidth, HEIGHT_CONDITION_BUTTON);
        [backView addSubview:self.clearButton];
        [backView addSubview:self.saveButton];
        
    }
    
    [self insertSubview:self.maskButton atIndex:0];
    
    self.frame = CGRectMake(0, primaryY, kWindowW, kWindowH - primaryY);
    self.scrollView.contentSize = CGSizeMake(0, localPrimaryY);
    self.scrollView.frame = CGRectMake(0, 0, kWindowW, localHeight);
    
    [superView addSubview:self];
    
    return primaryY + localPrimaryY;
}

/**
 * 是否未多级选项
 */
-(BOOL)isHashSubItems{
    
    if (self.conditionModel && self.conditionModel.selectItems) {
        
        for (int i = 0; i < self.conditionModel.selectItems.count; i++) {
            CSSelectableModel *selectableModel = [self.conditionModel.selectItems objectAtIndex:i];
            if (selectableModel.subItems && selectableModel.subItems > 0) {
                return YES;
            }
        }
    }
    return NO;
}


-(UIButton *)getConditionButton:(NSString *)conditionButton isSelected:(BOOL)isSelected{
    
    UIButton *button = [UIButton new];
    [button setTitle:conditionButton forState:UIControlStateNormal];
    button.titleLabel.font = [UIFont systemFontOfSize:14.0];
    button.titleLabel.textColor = COLOR_TEXT_1;
    button.layer.borderColor = COLOR_TOP_SEPERATOR_LINE.CGColor;
    button.layer.borderWidth = 1.0;
    [button setTitleColor:COLOR_TEXT_0 forState:UIControlStateNormal];
    button.frame = CGRectMake(0, 0, 0, HEIGHT_CONDITION_BUTTON);
    [button sizeToFit];
    
    button.frame = CGRectMake(0, 0, button.frame.size.width + 20, HEIGHT_CONDITION_BUTTON);
    [button addTarget:self action:@selector(conditionSelectClick:) forControlEvents:UIControlEventTouchUpInside];
    
    if(isSelected == YES) {
        
        [self styleSelected:button];
        
    }else{
        
        [self styleUnSelected:button];
        
    }
    
    return button;
}

-(void)select:(NSString *)code{
    
    for (int i = 0; i < self.selectableModels.count; i++) {
        CSSelectableModel *selectableModel = [self.selectableModels objectAtIndex:i];
        
        //如果两种相同的话
        if ([code isEqualToString:selectableModel.code]) {
            UIButton *button = [self.selectItemButtons objectAtIndex:i];
            selectableModel.isSelected = NO;
            [self doSelect:button];
            break;
        }
    }
}

//选中处理
-(void)doSelect:(UIButton *)selectedButton{
    
    for (int i = 0; i < self.selectItemButtons.count; i++) {
        
        CSSelectableModel *selectedModel = [self.selectableModels objectAtIndex:i];
        
        UIButton *button = [self.selectItemButtons objectAtIndex:i];
        
        if (button == selectedButton){
            
            if(selectedModel.isSelected == NO) {
                
                [self styleSelected:button];
                selectedModel.isSelected = YES;
                
                self.value = selectedModel.code;
                
            }else{
                
                [self styleUnSelected:button];
                selectedModel.isSelected = NO;
                self.conditionModel.value = selectedModel.code;
                self.conditionModel.contentValue = selectedModel.content;
            }
        }
    }
}

-(void)conditionSelectClick:(id)sender{
    
    [self doSelect:sender];
    
    for (int i = 0; i < self.selectItemButtons.count; i++) {
        
        UIButton *button = [self.selectItemButtons objectAtIndex:i];
        
        if (button == sender) {
            [self doSelect:[self.selectableModels objectAtIndex:i]];
        }
    }
}

-(void)styleUnSelected:(UIButton *)button{
    [button setTitleColor:COLOR_TEXT_1 forState:UIControlStateNormal];
    button.layer.borderColor = COLOR_TOP_SEPERATOR_LINE.CGColor;
    button.layer.borderWidth = 1.0;
    
    UIImageView *imageView = (UIImageView *)[button viewWithTag:123];
    if (imageView) {
        [imageView removeFromSuperview];
    }
}

-(void)styleSelected:(UIButton *)button{
    UIColor *color_Selected = COLOR_THEME;
    [button setTitleColor:color_Selected forState:UIControlStateNormal];
    button.layer.borderColor = color_Selected.CGColor;
    button.layer.borderWidth = 1.0;
    
    
    UIImageView *imageView = (UIImageView *)[button viewWithTag:123];
    
    if (imageView == nil) {
        UIImage *image = [self scaleToSize:[CSPageEngine getImageWithKey:KEY_ICON_CHECK] size:CGSizeMake(11, 11)];
        imageView = [[UIImageView alloc] initWithImage:image];
        imageView.frame = CGRectMake(button.frame.size.width - 11, 0, 11, 11);
        imageView.tag = 123;
        [button addSubview:imageView];
    }
}

-(void)clearClick{
    
    self.conditionModel.contentValue = @"";
    self.conditionModel.value = @"";
    
    for (int i = 0; i < self.selectItemButtons.count; i++) {
        
        UIButton *button = [self.selectItemButtons objectAtIndex:i];
        CSSelectableModel *selectableModel = [self.selectableModels objectAtIndex:i];
        
        selectableModel.isSelected = NO;
        [self styleUnSelected:button];
        
    }
    
}

-(void)saveClick{
    
    if (self.onConditionCongiureFinishedHandler) {
        
        NSString *codesString = @"";
        NSString *contentsString = @"";
        
        for (int i = 0; i < self.selectableModels.count; i++) {
            
            CSSelectableModel *selectableModel = [self.selectableModels objectAtIndex:i];
            
            if (selectableModel.isSelected) {
                
                if ([self isNullOrEmpty:codesString] && [self isNullOrEmpty:contentsString]) {
                    
                    codesString = selectableModel.code;
                    contentsString = selectableModel.content;
                    
                }else{
                    
                    codesString = [NSString stringWithFormat:@"%@,%@", codesString, selectableModel.code];
                    contentsString = [NSString stringWithFormat:@"%@,%@", contentsString, selectableModel.content];
                    
                }
            }
        }
        
        self.onConditionCongiureFinishedHandler(codesString, contentsString);
    }
    
}

-(NSMutableArray *)selectItemButtons{
    if (_selectItemButtons == nil) {
        _selectItemButtons = [NSMutableArray new];
    }
    return _selectItemButtons;
}

-(NSMutableArray *)selectableModels{
    if (_selectableModels == nil) {
        _selectableModels = [NSMutableArray new];
    }
    return _selectableModels;
}

-(UIScrollView *)scrollView{
    
    if (_scrollView == nil) {
        _scrollView = [UIScrollView new];
        _scrollView.bounces = NO;
        _scrollView.backgroundColor = [UIColor whiteColor];
        _scrollView.showsVerticalScrollIndicator = NO;
        [self addSubview:_scrollView];
    }
    return _scrollView;
    
}

-(UIButton *)clearButton{
    
    if (_clearButton == nil) {
        _clearButton = [[UIButton alloc] init];
        [_clearButton setTitle:@"清空" forState:UIControlStateNormal];
        [_clearButton addTarget:self action:@selector(clearClick) forControlEvents:UIControlEventTouchUpInside];
        STYLE_SAVE_BUTTON(_clearButton);
        
        _clearButton.backgroundColor = [UIColor colorFromHexCode:@"f5f5f5"];
        _clearButton.layer.borderColor = [UIColor colorFromHexCode:@"e4e4e4"].CGColor;
        _clearButton.layer.borderWidth = 1.0;
        [_clearButton setTitleColor:[UIColor colorFromHexCode:@"444444"] forState:UIControlStateNormal];
    }
    return _clearButton;
    
}

-(UIButton *)saveButton{
    
    if (_saveButton == nil) {
        _saveButton = [[UIButton alloc] init];
        [_saveButton setTitle:@"确定" forState:UIControlStateNormal];
        [_saveButton addTarget:self action:@selector(saveClick) forControlEvents:UIControlEventTouchUpInside];
         STYLE_SAVE_BUTTON(_saveButton);
    }
    return _saveButton;
    
}


@end
